<?php
/*
	[English learning system]
	NAME:admin_control
*/
if (!defined('IN_ELS'))
{
    die('Hacking attempt');
}
class control extends adminbase {
	var $pagecount 		= 0;
	var $currendnum 	= 1;
	var $pageSize 		= 15;	//每页多少条
	var $pageNumSize 	= 10;	//分页数
	
	function control() {
		$this->adminbase();
		
		$act=empty($_REQUEST['a']) ? 'empty' : $_REQUEST['a'];
		if($act=="login"){
			if($this->doLogin()){
//				$this->adminIframe();
				$this->pageRedirect("admin.php?p=admin");
			}else{
				$this->loginPage();
			}
			exit;
		}
		
		if($act=="empty") 				$this->adminIframe();
		if($act=="loginPage") 			$this->loginPage();
		if($act=="header")				$this->adminHeader();
		if($act=="menu")				$this->adminMenu();
		if($act=="logout")				$this->doLogout();
		if($act=="admin_editPassword")	$this->admin_editPassword();
		
	}
	
	function setErrInfo($key,$value){
		$this->view->assign($key,$value);
	}
	
	function adminIframe(){
	
		$this->view->display("admin_iframe");
	}
	
	function adminHeader(){
	
		$this->view->display("admin_header");
	}
	
	function adminMenu(){
		$this->view->display("admin_menu");
	}
	
	
	function loginPage(){
		$this->view->display("admin_login");
	}
	function doLogin(){
		$userName = $_REQUEST["username"];
		$passWord = $_REQUEST["password"];
		if(empty($userName)){
			$this->view->assign('errMessage'  ,"请确认账号");
			return false;
		}
		
		$this->view->assign('username'  ,$userName);
		
		if(empty($passWord)){
			$this->view->assign('errMessage'  ,"请确认密码");
			return false;
		}
		
		/*
		if($_REQUEST["validateCode"] != $_SESSION["validateCode"]){
			$this->view->assign('errMessage'  ,"验证码错误");
			return false;
		}
		unset($_SESSION["validateCode"]);
		*/
		
		$userInfo = $this->getLoginInfo($userName);
		if(empty($userInfo)){
			$this->view->assign('errMessage'  ,"用户名不存在");
			return false;
		}
		$passWord = md5($passWord); 
		if($passWord == $userInfo["password"]){
			$_SESSION["admin"] 		= $userInfo;
			return true;
		}else{
			$this->view->assign('errMessage'  ,"密码错误");
			return false;
		}
	}
	
	function doLogout(){
		session_destroy();
		$this->loginPage();
	}

	//修改密码
	function admin_editPassword(){
		$uid	 = $_SESSION["admin"]["uid"];
		$subPost = $_REQUEST["post"];
		if(!empty($subPost)){
			//点击确认按钮后的相应操作
			$password	= $_REQUEST["password"];
			$password2	= $_REQUEST["password2"];
			$password3	= $_REQUEST["password3"];
			
			//旧密码处理
			if(empty($password)){
				$this->view->assign('errMessage'  ,"请输入您的密码");
				$this->view->display("admin_editPassword");
				return false;
			}
			$userInfo = $this->getUserInfo($uid);
			
			$password  = md5($password);
			if($password != $userInfo["password"]){
				$this->view->assign('errMessage'  ,"请输入您的当前正确的密码");
				$this->view->display("admin_editPassword");
				return false;
			}
			
			//新密码处理
			if(empty($password2)){
				$this->view->assign('errMessage'  ,"请输入您的新密码");
				$this->view->display("admin_editPassword");
				return false;
			} else if(strlen($password2) < 5 || strlen($password2) > 16){
				$this->view->assign('errMessage'  ,"密码是由5位到16位");
				$this->view->display("admin_editPassword");
				return false;
			} else if($password2 != $password3){
				$this->view->assign('errMessage'  ,"两次密码不一致");
				$this->view->display("admin_editPassword");
				return false;
			}
			
			//入库
			$password2 = md5($password2);
			$dataArray					= array();
			$dataArray['password'] 		= $password2;
			$where = " uid = $uid";
			$this->upAdminMember($dataArray,$where);
			
			$this->view->assign('errMessage' ,"修改密码成功");
			$this->view->display("admin_editPassword");
		} else {
			$this->view->display("admin_editPassword");
		}
	}
	
}
?>